'''
Version: 0.0.1
Author: ider
Date: 2021-02-07 01:42:29
LastEditors: ider
LastEditTime: 2021-05-08 13:00:07
Description: 
'''
from pb import wikitext_pb2
from pb import wikitext_pb2_grpc
import grpc
from concurrent import futures
import time
import logging
import os
from wikitextparser import remove_markup
import multiprocessing
import re

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

rep = re.compile(r"\[|\]|\*|\{|\}")
def easyHandle(text):
    if isinstance(text,str):
        rs = rep.sub("", text)
        rs = rs.replace("&ndash","")
        return rs
    else:
        return ""

def textHandle(text):
    try:
        return len(remove_markup(text))
    except Exception as e:
        print("解析异常：",e,text)
        return len(easyHandle(text))
    return 

class WikiTextPlainServiceServicer(wikitext_pb2_grpc.WikiTextPlainServiceServicer):
    def __init__(self):
        self.pool = multiprocessing.Pool(processes=30)

    # 实现 proto 文件中定义的 rpc 调用
    def GetWikiTextPlain(self, request, context):

        result = self.pool.apply_async(textHandle,args=(request.text,))
        size = result.get()
        return wikitext_pb2.Visual_Count(count=size)

def server_start(port):
    MAX_MESSAGE_LENGTH = 1024 * 1024 * 1024
    # 定义开启4个线程处理接收到的请求
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=3),options=[
               ('grpc.max_send_message_length', MAX_MESSAGE_LENGTH),
               ('grpc.max_receive_message_length', MAX_MESSAGE_LENGTH)])
    # 将编译出来的rpc_pb2_grpc的add_HelloServiceServicer_to_server函数添加到server中
    wikitext_pb2_grpc.add_WikiTextPlainServiceServicer_to_server(WikiTextPlainServiceServicer(),server)
    
    # 定义服务端端口1234
    server.add_insecure_port(f'0.0.0.0:{port}')
    server.start()
    logger.info("start")
    server.wait_for_termination()

if __name__ == "__main__":
    theadCount = 10
    basicPort = 11120

    ps = []
    for i in range(theadCount):
        prot = i + basicPort
        p = multiprocessing.Process(target=server_start,args=(prot,))
        p.start()
        ps.append(p)

    try:
        while True:
            time.sleep(5000)
    except KeyboardInterrupt:
        for p in ps:
            p.terminate()
        exit(0)
    
